package com.easy.action;

import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import com.easy.bean.LayData;
import com.easy.bean.User;
import com.easy.service.Service;
import com.easy.service.impl.LoginService;
import com.easy.service.impl.UserService;
import com.easy.util.Sys;

/**
 * Servlet implementation class UserServlet
 */
@WebServlet("/user/*")
public class UserServlet extends HttpServlet {
	private Service userS=new UserService();
	private LoginService loginS=new LoginService();
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String uri=req.getRequestURI();
		//uri:  /hotelms/user/list
		uri=uri.substring(uri.lastIndexOf("/")+1);
		Map params = (Map)req.getAttribute("params");
		
		
		if("list".equals(uri)) {
			List<User> list=userS.queryList(params);
			int count=userS.queryCount(params);
			//封装成LayData对象
			//因为LayUI需要固定的格式来解析数据
			LayData lay=new LayData(count,list);
			//将lay对象转化成json数据,将json数据返回给前台
			String json=JSON.toJSONString(lay);
			resp.getWriter().write(json);
			return;
		}else if("login".equals(uri)) {
			String username = req.getParameter("username");
			String password = req.getParameter("password");
			User loginUser=loginS.query(username);
			if(loginUser!=null) {
				if(username.equals(loginUser.getUsername()) && password.equals(loginUser.getPassword())) {
				req.getSession().setAttribute(Sys.LOGIN, loginUser);
				if(loginUser.getRole().equals("酒店经理")) {
					req.getRequestDispatcher("/WEB-INF/page/admin.jsp").forward(req, resp);
				}
				else if (loginUser.getRole().equals("普通用户")) {
					req.getRequestDispatcher("/WEB-INF/page/useradmin.jsp").forward(req, resp);
				}
				else if (loginUser.getRole().equals("酒店员工")) {
					req.getRequestDispatcher("/WEB-INF/page/workeradmin.jsp").forward(req, resp);
				}
				else if (loginUser.getRole().equals("维修人员")) {
					req.getRequestDispatcher("/WEB-INF/page/repairadmin.jsp").forward(req, resp);
				}
				return ;
				} else {
					req.getRequestDispatcher("/login.jsp").forward(req, resp);
				}
			}
			else {
				req.getRequestDispatcher("/login.jsp").forward(req, resp);
			}
			
		}else if("register".equals(uri)) {
			String username = req.getParameter("username");
			String password = req.getParameter("password");
			String name = req.getParameter("name");
			String id_number = req.getParameter("id_number");
			String sex = req.getParameter("sex");
			String birth = req.getParameter("birth");
			String email = req.getParameter("email");
			String phone = req.getParameter("phone");
			userS.insert(params);
			req.getRequestDispatcher("/login.jsp").forward(req, resp);
		}else if("add".equals(uri)) {
			//获取数据,将参数传给service,service交给dao访问数据库
			DateFormat format = new SimpleDateFormat("yyyy-MM-dd"); 
			String username = req.getParameter("username");
			String password = req.getParameter("password");
			String name = req.getParameter("name");
			String id_number = req.getParameter("id_number");
			String sex = req.getParameter("sex");
			try {
				Date birth = format.parse(req.getParameter("birth"));
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			String u_state = req.getParameter("u_state");
			String email = req.getParameter("email");
			String phone = req.getParameter("phone");
			String role = req.getParameter("role");
			try {
				Date regtime = format.parse(req.getParameter("regtime"));
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			int u_integral = Integer.parseInt(req.getParameter("u_integral"));
			int count = userS.insert(params);
			resp.getWriter().write(count);
			return;
		}else if("del".equals(uri)) {
			
			
			//获取数据,将参数传给service,service交给dao访问数据库
			int u_id = Integer.parseInt(req.getParameter("u_id"));
			System.out.println(u_id);
			
			int count = userS.delete(params);
			resp.getWriter().write(count);
			return;
		}else if("upd".equals(uri)) {
			
			
			
			//获取数据,将参数传给service,service交给dao访问数据库
			DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
			int u_id = Integer.parseInt(req.getParameter("u_id"));
			System.out.println(u_id);
			String username = req.getParameter("username");
			String password = req.getParameter("password");
			String name = req.getParameter("name");
			String id_number = req.getParameter("id_number");
			String sex = req.getParameter("sex");
			try {
				Date birth = format.parse(req.getParameter("birth"));
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			String u_state = req.getParameter("u_state");
			String email = req.getParameter("email");
			String phone = req.getParameter("phone");
			String role = req.getParameter("role");
			try {
				Date regtime = format.parse(req.getParameter("regtime"));
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			int u_integral = Integer.parseInt(req.getParameter("u_integral"));
			int count = userS.update(params);
			resp.getWriter().write(count);
			return;
		}
	}
}
